﻿namespace Hidistro.UI.SaleSystem.CodeBehind
{
    using ASPNET.WebControls;
    using Hidistro.Core;
    using Hidistro.Core.Entities;
    using Hidistro.Membership.Context;
    using Hidistro.SaleSystem.Catalog;
    using Hidistro.UI.Common.Controls;
    using Hidistro.UI.SaleSystem.Tags;
    using System;
    using System.Collections.Specialized;
    using System.IO;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using System.Data.Common;
    using JSShop.Model;
    using JSShop.Enum;
    using System.Collections.Generic;

    [ParseChildren(true)]
    public class BrandDetails_new : HtmlTemplatedWebControl
    {
        private int brandId;
        private Hishop_BrandCategories brandCategory;
        private HtmlImage litBrandXx;
        private Literal litBrandName;
        private Literal litBrandName2;
        private Literal litBrandRemark;

        private wnxd_Shopping_LiList GoodsList;
        private wnxd_Pager Pager;
        protected override void AttachChildControls()
        {
            if (!int.TryParse(this.Page.Request.QueryString["brandId"], out this.brandId))
            {
                base.GotoResourceNotFound();
            }
            brandCategory = new Juse.Interface.ProductBrowser().GetBrandInfo(this.brandId);
            if (brandCategory == null)
            {
                this.Page.Response.Redirect(Globals.ApplicationPath + "/ResourceNotFound.aspx?errorMsg=" + Globals.UrlEncode("该品牌已经不存在"));
            }
            else if ((brandCategory != null) && !(string.IsNullOrEmpty(brandCategory.Theme) || !File.Exists(HiContext.Current.Context.Request.MapPath(HiContext.Current.GetSkinPath() + "/brandcategorythemes/" + brandCategory.Theme))))
            {
                this.SkinName = "/brandcategorythemes/" + brandCategory.Theme;
            }
            this.litBrandXx = (HtmlImage)this.FindControl("litBrandXx");
            this.litBrandName = (Literal)this.FindControl("litBrandName");
            this.litBrandName2 = (Literal)this.FindControl("litBrandName2");
            this.litBrandRemark = (Literal)this.FindControl("litBrandRemark");

            this.GoodsList = (wnxd_Shopping_LiList)this.FindControl("GoodsList");
            this.Pager = (wnxd_Pager)this.FindControl("pager");
            if (!this.Page.IsPostBack)
            {
                this.LoadCategoryHead();
                if (!string.IsNullOrEmpty(this.brandCategory.Logo))
                {
                    this.litBrandXx.Src = "http://image.juseshop.com"+this.brandCategory.Logo;
                }
                else
                {
                    this.litBrandXx.Src = "http://image.juseshop.com/images/xqbanner_03.png";
                }
                this.litBrandName.Text = brandCategory.BrandName;
                this.litBrandName2.Text = brandCategory.BrandName;
                this.litBrandRemark.Text = brandCategory.Description;
                this.BindBrandProduct();
            }
        }


        protected override void OnInit(EventArgs e)
        {
            if (this.SkinName == null)
            {
                this.SkinName = "Skin-BrandDetails_new.html";
            }
            base.OnInit(e);
        }
        private void LoadCategoryHead()
        {
            string cid =this.Page.Request.QueryString["cid"] == null ? "" : this.Page.Request.QueryString["cid"].ToString();
            string filter = this.Page.Request.QueryString["filter"] == null ? "" : this.Page.Request.QueryString["filter"].ToString();
            string brand = this.brandCategory.BrandName;
            string rank = this.Page.Request.QueryString["rank"] == null ? "0" : this.Page.Request.QueryString["rank"].ToString();
            if (string.IsNullOrEmpty(filter + cid))
            {
                if (!string.IsNullOrEmpty(this.brandCategory.MetaDescription))
                {
                    MetaTags.AddMetaDescription(this.brandCategory.MetaDescription, HiContext.Current.Context);
                }
                if (!string.IsNullOrEmpty(this.brandCategory.MetaKeywords))
                {
                    PageTitle.AddSiteNameTitle(this.brandCategory.MetaKeywords);
                }
            }
            else
            {
                //橘色 制服诱惑 护士装 全部 综合排序产品 第1页 【橘色商城】
                if (!string.IsNullOrEmpty(filter))
                {
                    switch (filter)
                    {
                        case "1":
                            {
                                filter = "100元及以下";
                                break;
                            }
                        case "2":
                            {
                                filter = "101-200元";
                                break;
                            }
                        case "3":
                            {
                                filter = "201-300元";
                                break;
                            }
                        case "4":
                            {
                                filter = "301-500元";
                                break;
                            }
                        case "5":
                            {
                                filter = "500元以上";
                                break;
                            }
                        case "0":
                            {
                                filter = "全部";
                                break;
                            }
                    }
                }
                switch (rank)
                {
                    case "0":
                        {
                            rank = "综合排序";
                            break;
                        }
                    case "1":
                        {
                            rank = "销量优先";
                            break;
                        }
                    case "3":
                        {
                            rank = "人气优先";
                            break;
                        }
                    case "6":
                        {
                            rank = "价格最低";
                            break;
                        }
                    case "5":
                        {
                            rank = "价格最高";
                            break;
                        }
                    case "7":
                        {
                            rank = "最新上架";
                            break;
                        }
                }

                if (!string.IsNullOrEmpty(cid))
                {
                    List<JSShop.Model.Hishop_Categories> brandCategories = new Juse.Interface.CategoryBrowser().GetBrandCategoryList(this.brandId, 16);
                    foreach (Hishop_Categories v in brandCategories)
                    {
                        if (v.CategoryId == int.Parse(cid))
                        {
                            cid = v.Name;
                            break;
                        }
                    }
                }
                else
                {
                    cid = "所属分类";
                }

                string name = brand + cid  + filter + rank + "产品第" + this.Pager.Index.ToString() + "页";

                MetaTags.AddMetaKeywords(name, HiContext.Current.Context);
                MetaTags.AddMetaDescription(name + this.brandCategory.MetaDescription, HiContext.Current.Context);
                PageTitle.AddSiteNameTitle(name);

            }
        }
        private Database database = DatabaseFactory.CreateDatabase();


        private ProductBrowseQuery GetProductBrowseQuery()
        {
            int num2;
            ProductBrowseQuery entity = new ProductBrowseQuery();
            if (int.TryParse(this.Page.Request.QueryString["categoryId"], out num2))
            {
                entity.CategoryId = num2;
            }
            entity.BrandId = this.brandId;
            
            if (int.TryParse(this.Page.Request.QueryString["feature"], out num2))
            {
                entity.feature = num2;
            }
            if (!string.IsNullOrEmpty(this.Page.Request.QueryString["keywords"]))
            {
                entity.Keywords = DataHelper.CleanSearchString(Globals.HtmlEncode(Globals.UrlDecode(this.Page.Request.QueryString["keywords"])));
            }
            if (int.TryParse(this.Page.Request.QueryString["filter"], out num2))
            {
                switch (num2)
                {
                    case 1:
                        {
                            entity.MaxSalePrice = 100;
                            break;
                        }
                    case 2:
                        {
                            entity.MinSalePrice = 100;
                            entity.MaxSalePrice = 200;
                            break;
                        }
                    case 3:
                        {
                            entity.MinSalePrice = 200;
                            entity.MaxSalePrice = 300;
                            break;
                        }
                    case 4:
                        {
                            entity.MinSalePrice = 300;
                            entity.MaxSalePrice = 500;
                            break;
                        }
                    case 5:
                        {
                            entity.MinSalePrice = 500;
                            break;
                        }
                }
            }
            entity.PageIndex = this.Pager.Index;
            entity.PageSize = this.Pager.Size;
            if (int.TryParse(this.Page.Request.QueryString["rank"], out num2))
            {
                switch (num2)
                {
                    case 1:
                        {
                            entity.SortBy = "ShowSaleCounts";
                            entity.SortOrder = SortAction.Desc;
                            break;
                        }
                    case 2:
                        {
                            entity.SortBy = "ShowSaleCounts";
                            entity.SortOrder = SortAction.Asc;
                            break;
                        }
                    case 3:
                        {
                            entity.SortBy = "VistiCounts";
                            entity.SortOrder = SortAction.Desc;
                            break;
                        }
                    case 4:
                        {
                            entity.SortBy = "VistiCounts";
                            entity.SortOrder = SortAction.Asc;
                            break;
                        }
                    case 5:
                        {
                            entity.SortBy = "SalePrice";
                            entity.SortOrder = SortAction.Desc;
                            break;
                        }
                    case 6:
                        {
                            entity.SortBy = "SalePrice";
                            entity.SortOrder = SortAction.Asc;
                            break;
                        }
                    case 7:
                        {
                            entity.SortBy = "AddedDate";
                            entity.SortOrder = SortAction.Desc;
                            break;
                        }
                    case 8:
                        {
                            entity.SortBy = "AddedDate";
                            entity.SortOrder = SortAction.Asc;
                            break;
                        }
                }
            }
            else
            {
                entity.SortBy = "DisplaySequence";
            }
            Globals.EntityCoding(entity, true);
            return entity;
        }
        private void BindBrandProduct()
        {
            JSShop.Model.DbQueryResult browseProductList = new Juse.Interface.ProductBrowser().GetSearchsProductByCategoryId(this.GetProductBrowseQuery());
            this.GoodsList.DataSource = browseProductList.Data;
            this.GoodsList.DataBind();
            this.Pager.Total = browseProductList.TotalRecords;
        }
    }
}

